﻿using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Hangfire;
using Hangfire.Dashboard;
using Hangfire.Redis;
using System.Configuration;

[assembly: OwinStartup(typeof(AVIS.TaskScheduler.Admin.Startup))]

namespace AVIS.TaskScheduler.Admin
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var connectionString = ConfigurationManager.ConnectionStrings["Hangfire.Redis"].ConnectionString;

            GlobalConfiguration.Configuration
                .UseRedisStorage(connectionString, new RedisStorageOptions
                {
                    Db = 2,
                })
                .UseDashboardMetric(DashboardMetrics.FailedCount);

            app.UseHangfireDashboard("/hangfire", new DashboardOptions
            {
                Authorization = new[] { new MyAuthorizationFilter() }
            });
        }
    }

    public class MyAuthorizationFilter : IDashboardAuthorizationFilter
    {
        public bool Authorize(DashboardContext context)
        {
            return true;
            //// In case you need an OWIN context, use the next line, `OwinContext` class
            //// is the part of the `Microsoft.Owin` package.
            //var owinContext = new OwinContext(context.GetOwinEnvironment());

            //// Allow all authenticated users to see the Dashboard (potentially dangerous).
            //return owinContext.Authentication.User.Identity.IsAuthenticated;
        }
    }
}
